package service

import (
	"gorm.io/gorm"
	"practice/config"
	"practice/internal/model/dao"
)

func TravelLists(title string, page, pageSize int) (travelList []*dao.Travel, err error) {
	err = config.DB.Where("title like ?", "%"+title+"%").Scopes(Paginate(page, pageSize)).Find(&travelList).Error
	return
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
